<?php
// 欢迎关注抖音
// 9- 5.15 cAg:/ 复制打开抖音，看看【LauEl的作品】⑦配套思维导图以及说明# 图书馆借读系统  https://v.douyin.com/DDGqhFN/

//模板申请：
//{{first.DATA}}
//今天天气：{{keyword1.DATA}}
//今天温度：{{keyword2.DATA}}
//我们已经恋爱了：{{keyword3.DATA}}
//距离小宝生日还有：{{keyword4.DATA}}
//{{remark.DATA}}

$appId = file_get_contents("appid.txt"); //对应自己的appId
$appSecret = file_get_contents("appsecret.txt"); //对应自己的appSecret
$wxgzhurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
$access_token_Arr = https_request($wxgzhurl);
$access_token = json_decode($access_token_Arr, true);
$ACCESS_TOKEN = $access_token['access_token']; //ACCESS_TOKEN

// 什么时候恋爱的(格式别错)
$lovestart_time=file_get_contents("lianai.txt");
$lovestart = strtotime($lovestart_time);
$end = time();
$love = ceil(($end - $lovestart) / 86400);

// 下一个生日是哪一天(格式别错)
$birthdaystart_time = file_get_contents("shengri.txt");
$birthdaystart = strtotime($birthdaystart_time);
$end = time();
$diff_days = ($birthdaystart - $end);
$birthday = (int)($diff_days/86400);
$birthday = str_replace("-", "", $birthday);

//天气接口api

$tianqi_appid=file_get_contents("tianqiappid.txt");
$tianqi_appsecret=file_get_contents("tianqiappsecret.txt");
$tianqi_city=file_get_contents("chengshi.txt");

//$tianqiurl = 'https://www.yiketianqi.com/free/day?appid=95943915&appsecret=5KNSKu9y&unescape=1&city=武汉'; //如果固定情况可以使用这个
$tianqiurl = 'https://www.yiketianqi.com/free/day?appid='.$tianqi_appid.'&appsecret='.$tianqi_appsecret.'&unescape=1&city='.$tianqi_city; //修改为自己的
//var_dump($tianqiurl);die;
$tianqiapi = https_request($tianqiurl);
$tianqi = json_decode($tianqiapi, true);
//
//这里去注册情话接口  alapi.cn注册一下，登录-接口管理-更新token秘钥 就有自己的了。
$qinghuatoken=file_get_contents("qinghuatoken.txt");
$qinghuaqiurl = 'https://v2.alapi.cn/api/qinghua?token='.$qinghuatoken; //修改为自己的
$qinghuaapi = https_request($qinghuaqiurl);
$qinghua = json_decode($qinghuaapi, true);

// 单独一个人接收————————————————————————————————————————————————————————————————————————————————————————————
// 你自己的一句话
$yjh=file_get_contents("huayu.txt");
$touser = 'o5gll6luXLKdt_sGiuuPKraJ_X4M';  //这个填你女朋友的openid
$data = array(
    'touser' => $touser,
    'template_id' => "4-TaSBcooMuVPSS6HmNIxkRd9jVBZ3xcIQYt84ByP00", //改成自己的模板id，在微信后台模板消息里查看
    'data' => array(
        'first' => array(
            'value' => $yjh,
            'color' => "#000"
        ),
        'keyword1' => array(
            'value' => $tianqi['wea'],
            'color' => "#000"
        ),
        'keyword2' => array(
            'value' => $tianqi['tem_day'].' °C',
            'color' => "#000"
        ),
        'keyword3' => array(
            'value' => $love . '天',
            'color' => "#000"
        ),
        'keyword4' => array(
            'value' => $birthday . '天',
            'color' => "#000"
        ),
        'remark' => array(
            'value' => $qinghua['data']['content'],
            'color' => "#FF0000"
        ),
    )
);
// 下面这些就不需要动了————————————————————————————————————————————————————————————————————————————————————————————
$json_data = json_encode($data);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $ACCESS_TOKEN;
$res = https_request($url, urldecode($json_data));
$res = json_decode($res, true);

if ($res['errcode'] == 0 && $res['errcode'] == "ok") {
    echo "发送成功！<br/>";
}else {
        echo "发送失败！请检查代码！！！<br/>";
}
// 单独一个人接收————————————————————————————————————————————————————————————————————————————————————————————
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

